<template>
    <div class="component-upload-image">
        <el-upload :action="uploadImgUrl" list-type="picture-card" :on-success="handleUploadSuccess"
            :before-upload="handleBeforeUpload" :on-error="handleUploadError" name="file" :show-file-list="false"
            :headers="headers" style="display: inline-block; vertical-align: top">
            <img v-if="value&&uploadType=='img'" :src="value" class="avatar">
            <video v-else-if="value&&uploadType=='video'" id="bgVideo" class="avatar" controls :src="value">
                <!-- <source :src="form.itemVideo" type="video/mp4" />
        您的浏览器不支持Video标签。 -->
            </video>
            <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
        <!-- 上传提示 -->
        <div class="el-upload__tip" slot="tip" v-if="showTip">
            请上传
            <template v-if="fileSize">
                大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b>
            </template>
            <template v-if="fileType">
                格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b>
            </template>
            的文件
        </div>
    </div>
</template>

<script>
    import {
        getToken
    } from '@/utils/auth'

    export default {
        components: {},
        props: {
            value: {
                type: String,
                default: ''
            },
            uploadType: {
                type: String,
                default: 'img'
            },
            // 数量限制
            limit: {
                type: Number,
                default: 1
            },
            // 大小限制(MB)
            fileSize: {
                type: Number,
                default: 300
            },
            // 文件类型, 例如['png', 'jpg', 'jpeg']
            fileType: {
                type: Array,
                default: () => ["mp4"]
            },
            // 是否显示提示
            isShowTip: {
                type: Boolean,
                default: true
            }
        },
        data() {
            return {
                uploadImgUrl: process.env.VUE_APP_BASE_API + '/common/upload', // 上传的图片服务器地址
                headers: {
                    Authorization: 'Bearer ' + getToken()
                },
                baseImgUrl: process.env.VUE_APP_BASE_API, // 图片URL
                fileList: []
            }
        },
        watch: {},
        computed: {
            // 是否显示提示
            showTip() {
                return this.isShowTip && (this.fileType || this.fileSize);
            }
        },
        methods: {
            handleUploadSuccess(res) {
                // this.fileList.push({
                //     name: res.fileName,
                //     url: this.baseImgUrl + res.url,
                //     resultUrl: res.url
                // });
                this.$emit('input', this.baseImgUrl + res.url)
                this.loading.close()
            },
            handleBeforeUpload(file) {
                let isImg = false;
                if (this.fileType.length) {
                    let fileExtension = "";
                    if (file.name.lastIndexOf(".") > -1) {
                        fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
                    }
                    isImg = this.fileType.some(type => {
                        if (file.type.indexOf(type) > -1) return true;
                        if (fileExtension && fileExtension.indexOf(type) > -1) return true;
                        return false;
                    });
                } else {
                    isImg = file.type.indexOf("image") > -1;
                }

                if (!isImg) {
                    this.$message.error(
                        `文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`
                    );
                    return false;
                }
                if (this.fileSize) {
                    const isLt = file.size / 1024 / 1024 < this.fileSize;
                    if (!isLt) {
                        this.$message.error(`上传视频大小不能超过 ${this.fileSize} MB!`);
                        return false;
                    }
                }
                this.loading = this.$loading({
                    lock: true,
                    text: "上传中",
                    background: "rgba(0, 0, 0, 0.7)"
                });




            },
            handleUploadError() {
                this.$message({
                    type: 'error',
                    message: '上传失败'
                })
                this.loading.close()
            },
            // 对象转成指定字符串分隔
            listToString(list, separator) {
                let strs = "";
                separator = separator || ",";
                list.forEach(item => {
                    strs += item.url + separator;
                });
                // for (let i in list) {
                //   strs += list[i].url + separator;
                // }
                return strs != "" ? strs.substr(0, strs.length - 1) : "";
            }
        }
    }

</script>

<style scoped lang="scss">
    .avatar {
        width: 100%;
        height: 100%;
    }

</style>
